package com.aaa.sm.service.impl;

import com.aaa.sm.entity.Vehicle;
import com.aaa.sm.dao.VehicleDao;
import com.aaa.sm.service.VehicleService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆(Vehicle)表服务实现类
 *
 * @author makejava
 * @since 2023-07-06 16:16:34
 */
@Service("vehicleService")
public class VehicleServiceImpl implements VehicleService {
    @Resource
    private VehicleDao vehicleDao;

    /**
     * 查询门店车牌
     * @param vehicleId
     * @return
     */
    @Override
    public List<Vehicle> queryA(Integer vehicleId) {
        return vehicleDao.queryA(vehicleId);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param vehicleId 主键
     * @return 实例对象
     */
    @Override
    public Vehicle queryById(Integer vehicleId) {
        return this.vehicleDao.queryById(vehicleId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<Vehicle> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMsg());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        Vehicle vehicle = page.getData();
        return new PageInfo(vehicleDao.queryAllByLimit(vehicle));
    }

    /**
     * 新增数据
     *
     * @param vehicle 实例对象
     * @return 实例对象
     */
    @Override
    public Vehicle insert(Vehicle vehicle) {
        this.vehicleDao.insert(vehicle);
        return vehicle;
    }

    /**
     * 修改数据
     *
     * @param vehicle 实例对象
     * @return 实例对象
     */
    @Override
    public Vehicle update(Vehicle vehicle) {
        this.vehicleDao.update(vehicle);
        return this.queryById(vehicle.getVehicleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param vehicleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer vehicleId) {
        return this.vehicleDao.deleteById(vehicleId) > 0;
    }
}
